package com.richard.cloud;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.alibaba.nacos.api.config.annotation.NacosConfigListener;
import com.richard.cloud.entity.Payment;
import com.richard.cloud.openfeign.PaymentServiceFeign;
import com.richard.springcloud.base.ComnResult;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class BootConsumerOrder91 {
	public static void main(String[] args) {
		ConfigurableApplicationContext ctx = SpringApplication.run(BootConsumerOrder91.class, args);
//		ConfigurableEnvironment env = ctx.getEnvironment();
//            try {
//            	while(true) {
//            		System.err.println(env.getProperty("test.config.info"));
//            		TimeUnit.SECONDS.sleep(2);
//            	}
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
	}
	
	@LoadBalanced
	@Bean
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}
	
	@RefreshScope
	@RestController
    public class TestController {
		@Value("${provider.payment-service-addr}")
		private String payment_server_addr;
		
		@Value("${test.config.info}")
		private String test_config_info;
		
        @GetMapping(value = "/hello")
        public String hello() {
            return test_config_info;
        }
        
        @Resource
		private RestTemplate restTemplate;
        
        @Resource
        private PaymentServiceFeign paymentServiceFeign;
        
        @GetMapping("/consumer/payment/{id}")
        public Payment payment(@PathVariable Long id) {
            //Payment resp = restTemplate.getForObject(payment_server_addr + "/payment/" + id, Payment.class);
        	Payment resp = paymentServiceFeign.getPaymentInfo(id);
			return resp;
        }
        
        @Value("${spring.application.name}")
		private String appName;
		
		@Value("${spring.profiles.active}")
		private String active;
		
        @NacosConfigListener(dataId = "consumer-order-test.yml")
        public void onNacosConfigChange(String config) {
        	System.err.println("config changed: " + config);
        }
    }
}
